package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.payconfig.EntityPackageType;
import com.ocom.common.entity.payconfig.EntityPackageVoucher;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.callpackage.OnShowPackageDetail;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoPackageVoucher;
import com.ocom.payconfig.service.PackageVoucherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PackageVoucherServiceImpl extends ServiceImpl<IDaoPackageVoucher, EntityPackageVoucher> implements PackageVoucherService {

    @Autowired
    private IDaoPackageVoucher daoPackageVoucher;

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public EntityPackageVoucher selectVoucherTypeId(Long comId, Long voucherTypeId) {
        QueryWrapper<EntityPackageVoucher> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq("voucher_type_id",voucherTypeId);
        queryWrapper.last(" limit 1");
        return this.getOne(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE,companyId = "comId")
    public List<OnShowPackageDetail> selectByComId(Long comId) {
        return daoPackageVoucher.selectByComId(comId);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId")
    public boolean deleteByPackageTypeId(Long comId, Long packageTypeId) {
        QueryWrapper<EntityPackageVoucher> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq("package_type_id", packageTypeId);
        return this.remove(queryWrapper);
    }


}
